package com.sample.javabean;
/**
 * 家庭档案类
 * @author 深深
 * @version 1.0
 */
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.sample.action.form.FamilyrecordmessageForm;
import com.sample.jdbc.JdbcUtils;
import com.sample.util.Pager;

public class Familyrecordmessage {
	private static final Log LOG=LogFactory.getLog(Familyrecordmessage.class);
	private JdbcUtils jdbcUtils;
	private String	countyid;//县级编号
	private String	townid;//镇级编号
	private String	groupid	;//组编号
	private String	family_id;//家庭编号
	private String	familymaster;//该家庭户主
	private String	cardNo;//户主身份证号
	private String	villageid;//所在村编号
	private String	buildtime;//建档时间
	private String	id;//主键
	
 /**
  * 查询家庭档案信息全部
  * @return
  */
	public List<Familyrecordmessage> findAll(){
		Connection conn=null;
		Statement stmt =null;
		ResultSet rs =null;
		List<Familyrecordmessage> familyrecordmessageList =new ArrayList<Familyrecordmessage>(20);
		try{
			 conn =jdbcUtils.getConn();
			 stmt =conn.createStatement();
			//构造SQL语句
			String sql="select * from t_family_recordmessage where 1=1";
			 rs =stmt.executeQuery(sql);
			while(rs.next()){
				String countyid=rs.getString("countyid");
				String townid =rs.getString("townid");
				String groupid =rs.getString("groupid");
				String family_id=rs.getString("family_id");
				String familymaster =rs.getString("familymaster");
				String cardNo =rs.getString("cardNo");
				String villageid=rs.getString("villageid");
				String buildtime =rs.getString("buildtime");
				String id =rs.getString("id");
				Familyrecordmessage model=new Familyrecordmessage(countyid, townid,groupid,family_id,familymaster,cardNo,villageid,buildtime,id);
				familyrecordmessageList.add(model);
				
			}
			
		}catch (Exception e) {
			e.printStackTrace();
			LOG.error("load Familyrecordmessage list failed!", e);
		}finally{
			jdbcUtils.close(conn, stmt,rs);
		}
		return familyrecordmessageList;
		
	}
	
	/**
	 * 加载家庭档案信息
	 * @throws Exception
	 */
	public void load()throws Exception{
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs =null; 
		try {
			conn = jdbcUtils.getConn();
			stmt = conn.createStatement();
			// 3:构造插入SQL
			String sql = "select * from  t_family_recordmessage where family_id='"+this.family_id+"'";
			// 4:执行SQL并返回结果
			rs=stmt.executeQuery(sql);
			if(rs.next()){
				this.countyid =rs.getString("countyid");
				this.townid =rs.getString("townid");
				this.groupid =rs.getString("groupid");
				this.family_id =rs.getString("family_id");
				this.familymaster =rs.getString("familymaster");
				this.cardNo =rs.getString("cardNo");
				this.villageid =rs.getString("villageid");
				this.buildtime =rs.getString("buildtime");
				this.id =rs.getString("id");
				
			}
			// 5:关闭资源
		} catch (SQLException e) {
			e.printStackTrace();
			throw new SQLException("加载家庭档案信息失败！");
		} finally {
			jdbcUtils.close(conn, stmt);
		}	
	}
	
	/**
	 * 家庭档案信息修改
	 */
	public void editFamilyrecordmessage() {

		Connection conn = null;
		Statement stmt = null;
		try {
			conn = jdbcUtils.getConn();
			stmt = conn.createStatement();
			// 3:构造插入SQL
			String sql = "update  t_family_recordmessage set countyid='"+this.countyid+"',townid='"+this.townid+"',groupid='"+this.groupid+"',family_id='"+this.family_id+"',familymaster='"+this.familymaster+"',villageid='"+this.villageid+"',buildtime='"+this.buildtime+"' where family_id='"+this.family_id+"'";
			// 4:执行SQL并返回结果
			stmt.executeUpdate(sql);
			// 5:关闭资源
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			jdbcUtils.close(conn, stmt);
		}

	}
	
	/**
	 * 删除家庭档案信息
	 */
	public void delFamilyrecordmessage() {

		Connection conn = null;
		Statement stmt = null;
		try {
			conn = jdbcUtils.getConn();
			stmt = conn.createStatement();
			// 3:构造插入SQL
			String sql = "delete from t_family_recordmessage  where family_id='"+this.family_id+"'";
			// 4:执行SQL并返回结果
			System.out.println("del familyrecordmessage");
			stmt.executeUpdate(sql);
			// 5:关闭资源
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			jdbcUtils.close(conn, stmt);
		}

	}
	
	/**
	 * 添加家庭档案信息
	 */
	public void add(){
		Connection conn =null;
		Statement stmt=null;
		try{
			//获得连接对象	
			 conn =jdbcUtils.getConn();
			//定义SQL
			String sql="insert into t_family_recordmessage(countyid,townid,groupid,family_id,familymaster,cardNo,villageid,buildtime) values('"+this.countyid+"','"+this.townid+"','"+this.groupid+"','"+this.family_id+"','"+this.familymaster+"','"+this.cardNo+"','"+this.villageid+"','"+this.buildtime+"')";
			//Statement发送SQL语句给数据库
			 stmt =conn.createStatement();
			stmt.executeUpdate(sql);
		}catch (Exception e) {
			e.printStackTrace();
			
		}finally{
			jdbcUtils.close(conn, stmt);
			
		}
		
	}
	/**
	 * 分页显示家庭档案信息
	 * @param page
	 * @param size
	 * @param form
	 * @return
	 */
	public Pager<Familyrecordmessage> list(int page,int size,FamilyrecordmessageForm form) {

		Connection conn = null;
		Statement stmt = null;
		ResultSet rs =null;
		Pager<Familyrecordmessage> results=new Pager<Familyrecordmessage>();
		results.setPage(page);
		results.setSize(size);
		try {
			conn = jdbcUtils.getConn();
			stmt = conn.createStatement();
			int start=(page-1)*size;
			int limit=size;
			//查询SQL
			String selectSql="select * from t_family_recordmessage where 1=1";
			//模糊查询家庭户主
			if(form!=null&&form.getFamilymaster()!=null&&!"".equals(form.getFamilymaster())){
				selectSql +=" and familymaster like'%"+form.getFamilymaster()+"%'";
			}
			
			//统计SQL
			String countSql ="select count(*)as c from ("+selectSql+") as t1";
			//分页SQL
			 String pageSql=selectSql+" limit "+start+","+limit;
			 rs= stmt.executeQuery(countSql);
			 int total=0;
			 while(rs.next()){
				 total=rs.getInt("c");
			 }
			 results.setTotal(total);
			 //查询分页信息
			 rs= stmt.executeQuery(pageSql);
			 while(rs.next()){
						
				  String countyid=rs.getString("countyid");
				  String townid=rs.getString("townid");
				  String groupid=rs.getString("groupid");
				  String family_id=rs.getString("family_id");
				  String familymaster=rs.getString("familymaster");
				  String cardNo=rs.getString("cardNo");
				  String villageid=rs.getString("villageid");
				  String buildtime=rs.getString("buildtime");
				  String id=rs.getString("id");

				  Familyrecordmessage model=new Familyrecordmessage();
				  model.setCountyid(countyid);
				  model.setTownid(townid);
				  model.setGroupid(groupid);
				  model.setFamily_id(family_id);
				  model.setFamilymaster(familymaster);
				  model.setCardNo(cardNo);
				  model.setVillageid(villageid);
				  model.setBuildtime(buildtime);
				  model.setId(id);
				  results.getDataSets().add(model);
			 }
			 results.init();
			// 5:关闭资源
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			jdbcUtils.close(conn, stmt);
		}
		return results;

	}
//get/set方法
	   public Familyrecordmessage(){
			
			jdbcUtils =new JdbcUtils();
		}
		public String getCountyid() {
			return countyid;
		}
		public void setCountyid(String countyid) {
			this.countyid = countyid;
		}
		public String getTownid() {
			return townid;
		}
		public void setTownid(String townid) {
			this.townid = townid;
		}
		public String getGroupid() {
			return groupid;
		}
		public void setGroupid(String groupid) {
			this.groupid = groupid;
		}
		public String getFamily_id() {
			return family_id;
		}
		public void setFamily_id(String family_id) {
			this.family_id = family_id;
		}
		public String getFamilymaster() {
			return familymaster;
		}
		public void setFamilymaster(String familymaster) {
			this.familymaster = familymaster;
		}
		public String getCardNo() {
			return cardNo;
		}
		public void setCardNo(String cardNo) {
			this.cardNo = cardNo;
		}
		public String getVillageid() {
			return villageid;
		}
		public void setVillageid(String villageid) {
			this.villageid = villageid;
		}
		public String getBuildtime() {
			return buildtime;
		}
		public void setBuildtime(String buildtime) {
			this.buildtime = buildtime;
		}
		public String getId() {
			return id;
		}
		public void setId(String id) {
			this.id = id;
		}
		
		//构造方法
		public Familyrecordmessage(String countyid, String townid, String groupid,
				String family_id, String familymaster, String cardNo,
				String villageid, String buildtime, String id) {
			super();
			this.countyid = countyid;
			this.townid = townid;
			this.groupid = groupid;
			this.family_id = family_id;
			this.familymaster = familymaster;
			this.cardNo = cardNo;
			this.villageid = villageid;
			this.buildtime = buildtime;
			this.id = id;
		}
}
